<?php

class ProductController extends Controller
{
	public function filters()
	{
		return array(
			'rights', // perform access control for CRUD operations

		);
	}
	public function init(){
		Yii::app()->theme = 'admincp';
		$this->layout = '//layouts/admincp';
	}
	public function actionIndex()
	{
		$criteria = new CDbCriteria();
		$criteria->order = "id DESC";
		if(isset($_GET['name'])){
			$criteria->compare('name',$_GET['name'],true);
		}
		
		$count=Products::model()->count($criteria);
		$pages=new CPagination($count);

		// results per page
		$pages->pageSize=7;
		$pages->applyLimit($criteria);
		
		$model = Products::model()->findAll($criteria);
		$this->pageTitle = "Danh sách sản phẩm";
		$this->render('index',compact('model','pages'));
	}
	
	public function actionCreate(){
		$model = new Products;
		if(!empty($_POST['Products'])){
			$this->savePost($_POST['Products'],$model);
		}
		$this->pageTitle = "Thêm mới sản phẩm";
		$categories = Categories::model()->findAll(array('order'=>'id DESC'));
		$categories = CHtml::listData($categories, 'id', 'name');
		$this->render('create',compact('model','categories'));
	}
	
	public function actionUpdate($id = null){
		$model = Products::model()->findByPk($id);
		if($model === null)
			throw new CHttpException('404','Không tìm thấy trang');
		
		if(!empty($_POST['Products'])){
			$this->savePost($_POST['Products'],$model);
		}
		
		$categories = Categories::model()->findAll(array('order'=>'id DESC'));
		$categories = CHtml::listData($categories, 'id', 'name');
		$this->pageTitle = "Cập nhật sản phẩm";
		$this->render('update',compact('model','categories'));
	}
	
	public function savePost($data,$model){
		$model->attributes = $data;
		$model->content = $data['content'];
		if($model->isNewRecord){
			$model->created = time();
			$model->alias = time();
		}
		
		$fileUpload = CUploadedFile::getInstance($model,'image');
		if($fileUpload != null){
			$fileName = $fileUpload->name;
			if(file_exists(Yii::getPathOfAlias('webroot').'/upload/products/'.$fileName)){
				$fileName = explode('.',$fileName);
				$fileName = $fileName[0].'_'.time().'.'.$fileName[count($fileName)-1];
			}
			$model->image = $fileName;
		}
		
		if($model->save()){
			if($fileUpload != null){
				$fileUpload->saveAs(Yii::getPathOfAlias('webroot').'/upload/products/'.$fileName);
			}
			if($model->isNewRecord){
				Yii::app()->user->setFlash('success', "Thêm mới thành công.");
			}else{
				Yii::app()->user->setFlash('success', "Cập nhật thành công.");
			}
			$this->redirect(PIUrl::createUrl('/admincp/product'));
		}
		
	}
	public function actionDelete($id = null){
		$model = Products::model()->findByPk($id);
		if($model === null)
			throw new CHttpException('404','Không tìm thấy trang');
		if($model->delete()){
			Yii::app()->user->setFlash('success', "Xóa thành công.");
		}else{
			Yii::app()->user->setFlash('error', "Xóa thất bại.");
		}
		$this->redirect(PIUrl::createUrl('/admincp/product'));
	}
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}